home *** CD-ROM | disk | FTP | other *** search
- /* UltraDemo.c -- Exercise the functions of the Ultra library. Native
- Floating Point not used. */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include "Ultra.h" /* Dont't forget to include this file! */
-
- double total;
- long PascalRow10[11];
- FILE *fpout;
-
- /* The following procedure is an interesting, albeit weak, test of the
- randomness of the bits produced by Ultra. Here, we flip 10 coins 10 million
- times to see whether we can reproduce the tenth row of Pascal's Triangle
- (see UltraDemo.out, row 2 for correct answer). The seeds below give a
- perfect result, after normalization. In a series of ten tries, I got a
- perfect result four times. This test takes about 5 minutes on an
- unaccelerated MacIIci <TM>. */
-
- void OneSimpleTest()
- {
- double denom;
- long i;
- int j,k;
-
- for (i=0;i<10000000;i++) {
- k = 0;
- for (j=0;j<10;j++)
- k += Ultra_int1(); /* Heads or Tails? */
- PascalRow10[k]++;
- }
- for (j=0;j<=10;j++)
- fprintf(fpout," %ld",PascalRow10[j]); /* raw counts */
- fprintf(fpout,"\n\n");
- denom = (PascalRow10[0] + PascalRow10[10])/2.0;
- for (j=0;j<=10;j++) /* normalize and round to nearest integer */
- PascalRow10[j] = (int)(PascalRow10[j]/denom + 0.5);
- for (j=0;j<=10;j++)
- fprintf(fpout," %ld",PascalRow10[j]); /* normalized row 10 */
- fprintf(fpout,"\n\n");
- }
-
- void Exercise(int run) /* runtime = about 15 seconds */
- {
- double total;
- float mean,sigma;
- long i,calls[16];
- int k;
-
- total = 0.0;
- for (k=0;k<16;k++)
- calls[k] = 0;
- for (i=0;i<500000;i++) {
- k = Ultra_int7() & 15;
- calls[k]++;
- switch (k) {
- case 0:
- total += (double)Ultra_long32();
- break;
- case 1:
- total += (double)Ultra_long31();
- break;
- case 2:
- total -= (double)Ultra_long31();
- break;
- case 3:
- total += (double)Ultra_int16();
- break;
- case 4:
- total += (double)Ultra_int15();
- break;
- case 5:
- total -= (double)Ultra_int15();
- break;
- case 6:
- total += (double)Ultra_int8();
- break;
- case 7:
- total += (double)Ultra_int8u();
- break;
- case 8:
- total += (double)Ultra_int7();
- break;
- case 9:
- total += (double)Ultra_int1();
- break;
- case 10:
- total += (double)Ultra_uni();
- break;
- case 11:
- total += (double)Ultra_vni();
- break;
- case 12:
- total += (double)Ultra_duni();
- break;
- case 13:
- total += (double)Ultra_dvni();
- break;
- case 14:
- mean = Ultra_vni();
- sigma = Ultra_uni();
- total += (double)Ultra_norm(mean,sigma);
- break;
- case 15:
- total += (double)Ultra_expo(Ultra_uni());
- break;
- }
- }
- fprintf(fpout,"Result after run #%d: %21.18le\n",run,total);
- fprintf(fpout,"call array -->\n");
- for (k=0;k<16;k++)
- fprintf(fpout," %6ld",calls[k]);
- fprintf(fpout,"\n\n");
- }
-
- main()
- {
- FILE *fp;
-
- Ultra_seed1 = 12345678; Ultra_seed2 = 87654321;
- /* or use something like the following for variable input -->
- printf("Input two seeds: "); scanf("%lu %lu",&Ultra_seed1,&Ultra_seed2); */
- fpout = fopen("UltraDemo.out","w");
- Ultra_Init(); /* Critical! */
- OneSimpleTest();
- printf("Simple Test Complete\n");
- Ultra_RecallStart(); /* saved by Ultra_Init() */
- fp = fopen("UltraTemp.dat","w");
- fwrite((void *)Ultra_Remember,UltraSize,UltraLength,fp); /* there */
- fclose(fp);
- Exercise(1);
- printf("Exercise 1 Complete\n");
- fp = fopen("UltraTemp.dat","r");
- fread((void *)Ultra_Remember,UltraSize,UltraLength,fp);
- fclose(fp);
- Ultra_RecallStart(); /* and back again */
- Exercise(2);
- printf("Exercise 2 Complete\n");
- fclose(fpout);
- }
-